package com.chandon.juc.threadPool;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 单线程的定时任务线程池，用法与ScheduleThreadPool一致
 *
 * @author Chandon
 */

public class TestSingleThreadScheduledExecutor {
	public static void main(String[] args) {

		ScheduledExecutorService singleThreadScheduledThreadPool =
				Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("namedSingleThreadScheduledThreadFactory"));


		singleThreadScheduledThreadPool.execute(() ->
				System.out.println(Thread.currentThread().getName() + " 任务1")
		);

		singleThreadScheduledThreadPool.schedule(() -> {
			System.out.println(Thread.currentThread().getName() + " 任务2");
		}, 2, TimeUnit.SECONDS);

		singleThreadScheduledThreadPool.scheduleAtFixedRate(() -> {
			System.out.println(Thread.currentThread().getName() + " 任务3");
		}, 2, 4, TimeUnit.SECONDS);

		singleThreadScheduledThreadPool.scheduleWithFixedDelay(() -> {
			System.out.println(Thread.currentThread().getName() + " 任务4");
		}, 2, 4, TimeUnit.SECONDS);
	}

}
